#include<iostream>
#include<deque>
using namespace std;
const int N = 1000005;
deque<int> q;
int a[N];
int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) 
	{
		cin >> a[i];
	}
	for (int i = 0; i < n; i++)
	{
		while (!q.empty() && a[q.back()] >= a[i]) 
		{
			q.pop_back();
		}

		q.push_back(i);

		if (i - q.front() >= m)
		{
			q.pop_front();
		}

		if (i >= m - 1)
		{
			cout << a[q.front()] << " ";
		}
	}
	cout << endl;

	while (!q.empty())
	{
		q.pop_back();
	}
	for (int i = 0; i < n; i++)
	{
		while (!q.empty() && a[q.back()] <= a[i])
		{
			q.pop_back();
		}
		q.push_back(i);
		if (i - q.front() >= m)
		{
			q.pop_front();
		}
		if (i >= m - 1)
		{
			cout << a[q.front()] << " ";
		}
	}
	cout << endl;
	return 0;
}